/**
 * @file 033.变位词组.cc
 * @author snow-tyan (zziywang@163.com)
 * @brief {Life is too short to learn cpp.}
 * @version 0.1
 * @date 2021-12-11
 *
 * @copyright Copyright (c) 2021
 *
 */

#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

template <class T>
void print(const T &t)
{
    typename T::const_iterator it = t.begin();
    while (it != t.end()) {
        cout << *it++ << " ";
    }
    cout << endl;
}

class Solution
{
public:
    vector<vector<string>> groupAnagrams(vector<string> &strs)
    {
        // key 26个0，val 相同的变位词
        unordered_map<string, vector<string>> mp;
        for (string &str : strs) {
            string cnt(26, 0); // nb-plus
            for (char &ch : str) {
                ++cnt[ch - 'a'];
            }
            mp[cnt].emplace_back(str);
        }
        vector<vector<string>> res;
        for (auto it = mp.begin(); it != mp.end(); ++it) {
            res.emplace_back(it->second);
        }
        return res;
    }
};

int main()
{
    vector<string> strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
    auto res = Solution().groupAnagrams(strs);
    for (auto &r : res) {
        print(r);
    }
    return 0;
}
